home *** CD-ROM | disk | FTP | other *** search
- Path: waldorf.csc.calpoly.edu!galaxy!ostoll
- From: ostoll@galaxy.csc.calpoly.edu (Oliver Stoll)
- Newsgroups: comp.lang.c++
- Subject: Re: for (int i(1); ...)
- Date: 6 Mar 1996 07:22:38 GMT
- Organization: Cal Poly Computer Science Dept.
- Message-ID: <4hjefu$sbd@waldorf.csc.calpoly.edu>
- References: <4gg2j6$93g@darkstar.UCSC.EDU> <ltwx5fuqn8.fsf@kitz.inferenzsysteme.informatik.th-darmstadt.de>
- NNTP-Posting-User: ostoll@galaxy.csc.calpoly.edu
- X-Newsreader: TIN [version 1.2 PL2]
-
- Enno Sandner (enno@inferenzsysteme.informatik.th-darmstadt.de) wrote:
- : In article <4gg2j6$93g@darkstar.UCSC.EDU> ray@cse.ucsc.edu (Ray Swartz) writes:
-
- : I noticed that the syntax
-
- : for (int i(1); ...)
-
- : works just like
-
- : for (int i = 1; ...)
-
-
- : however, I've not seen this used anywhere.
-
- : This seems odd to me since initializing by assignment is seen as misleading
- : when applied to objects.
-
- : Should
-
- : for (int i(1);...)
-
-
- : even be used?
-
- : That's just a matter of taste, because both solutions are exactly the same.
- : In general an expression like 'AClass A_obj=another_A_obj;' will perform a
- : copy-construction of an 'AClass' object and won't result in construction +
- : subsequent assignment.
-
- : Enno
-
- Actually, for the trivial case of an integer, yes. If you should have an
- iterator for a class which for example contains a list and switches over the
- list with ++ and there is a lot of memory allocation involved, i would well
- prefer the copyconstruction of it rather than an assignment. In the case of
- an assignment, both destructor and copy constructor have to be called.
-
- At htis point i just wanted to mention another incompatibility I have
- noticed. There is a difference between AIX's xlC and g++ as for that
- definition (so if you want to write portable code, define the variable at
- the beginning of the function;):
-
- In xlC the scope of a variable defined in for(int i = 0, ...) is the
- complete function the for() statement is contained in. In g++ however, the
- validity of the int declared like that is only the for statement. So if you
- write a program on AIX using i after the for statement to decide the end
- vlue of the for perhaps, and compile the program on xlC it will report you
- an undefined variable. And if you use the same i with for(int i...) in two
- subsequent for calls with g++ AIX will tell you in the second for statement
- that i is already defined so...
-
- don't use for(int i...), either way ;)
-
-
- Oliver
-
-
-
- -------------------------------------------------------------------------------
- Oliver "Oyl McDoyle" Stoll
-
- Fachhochschule Karlsruhe California Polytechnic State
- stol0012@fh-karlsruhe.de University (San Luis Obispo)
- http://www.fh-karlsruhe.de/~stol0012 ostoll@galaxy.csc.calpoly.edu
-